package cn.yunyichina.openapi.proxy.filter.in;

import cn.yunyichina.openapi.proxy.filter.in.entity.InRequest;
import cn.yunyichina.openapi.proxy.filter.in.entity.InResponse;
import cn.yunyichina.utils.commons.StringUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by kevin on 2018/3/1.
 */
public class InFilterChain {
    //用List集合来存储过滤规则
    List<InFilter> filters = new ArrayList<InFilter>();

    public void addFilter(InFilter inFilter){
        filters.add(inFilter);
    }

    public InResponse doFilter(InRequest inRequest){
        if(null==inRequest){
            throw new NullPointerException("outrequest can not be null");
        }

        if(StringUtils.isBlank(inRequest.getJsonStr())){
            return InResponse.setResponse(inRequest.getJsonStr());
        }

        InResponse inResponse=new InResponse();
        for(int i=0;i<filters.size();i++){
            InFilter inFilter=filters.get(i);
            inResponse=inFilter.doFilter(inRequest);
            inRequest.setJsonStr(inResponse.getJsonStr());
        }
        return inResponse;
    }
}
